package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckgroupDao;
import com.itheima.health.dao.Checkgroup_CheckitemDao;
import com.itheima.health.dao.Setmeal_CheckgroupDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckgroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CheckgroupServiceImpl implements CheckgroupService {
    @Resource
    private CheckgroupDao checkgroupDao;
    @Resource
    private Checkgroup_CheckitemDao checkgroup_checkitemDao;
    @Resource
    private Setmeal_CheckgroupDao setmeal_checkgroupDao;

    @Override
    public PageResult SelectPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> page = checkgroupDao.SelectPage(queryPageBean.getQueryString());
        return PageResult.builder().rows(page.getResult()).total(page.getTotal()).build();
    }

    @Override
    public void Add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkgroupDao.Add(checkGroup);
        if (checkitemIds == null) {
            return;
        }
        checkgroup_checkitemDao.Add(checkGroup.getId(), checkitemIds);
    }
    @Override
    public CheckGroup findById(Integer id) {
        return checkgroupDao.findById(id);
    }

    @Override
    public void update(CheckGroup checkGroup) {
        System.out.println(checkGroup);
        checkgroupDao.update(checkGroup);
    }
    @Transactional
    @Override
    public void DeleteGroupById(Integer id) {
        int Count = setmeal_checkgroupDao.SelectByGroupId(id);
        if(Count>0) return;
        checkgroupDao.DeleteGroupById(id);
        checkgroup_checkitemDao.DeleteByGroupId(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkgroupDao.findALl();
    }
}
